﻿var note = (function () {
    var defaults = {
        container: "",
        item: "",
        second: 3,
        labtime: ""
    };
    var options = {}, method = {};

    var global = {
        items: {},
        count: 0,
        index: 0,
        current: function () {
            return this.items[this.index];
        },
        next: function () {
            return this.items[this.index + 1] ? this.items[this.index + 1] : this.items[0];
        }
    };

    var load = function (item, callback) {
        item = $(item);
        var tmpl = item.attr("tmpl");
        var datatype = item.attr("datatype");
        var type = item.attr("type");
        var labtime = $(options.labtime);

        //get data from remote server by ajax
        sp.ajax.call("common", {
            FunType: "notepad", DataType: datatype, type: type
        }, function (res) {
            item.html($(tmpl).tmpl(res.Data));
            labtime.html(res.Data[0].datetime);

            callback(item);
        }, function (res) { alert(res); });
    }

    method.init = function (opt) {
        //init options
        options = $.extend(defaults, opt);
        global.items = $(options.container).find(options.item);
        global.count = global.items.length;
        global.index = 0;

        load(global.current(), function (item) { });
        //start timer
        setInterval('note.next()', options.second * 1000);
    }

    method.next = function () {

        load(global.next(), function (item) {
            $(global.current()).fadeOut(150);
            item.fadeIn(800);

            var ind = global.index + 1;
            global.index = global.count == ind ? 0 : ind;
        });
    }

    return method;
} ());